package com.sz.demo;

import com.sz.demo.model.Fruit;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车类
 * @author Lenovo
 */
public class ShoppingCart {
    private List<Fruit> items;

    public ShoppingCart() {
        this.items = new ArrayList<>();
    }

    public void addItem(Fruit fruit, int quantity) {
        if (quantity > 0) {
            // 添加指定数量的水果到购物车
            for (int i = 0; i < quantity; i++) {
                items.add(fruit);
            }
        }
    }

    public List<Fruit> getItems() {
        return new ArrayList<>(items);
    }

    /**
     * 计算购物车中所有商品的总价
     */
    public BigDecimal calculateSubtotal() {
        BigDecimal subtotal = BigDecimal.ZERO;
        for (Fruit fruit : items) {
            subtotal = subtotal.add(fruit.calculatePrice(1));
        }
        return subtotal;
    }
}
